package com.wlyuan.open.scanner.dao.write;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.dao.entity.WaybillDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-12-13
 */
public interface WaybillWriteMapper extends BaseMapper<WaybillDO> {

    @Update("TRUNCATE TABLE t_waybill")
    void truncate();

    @Update("insert into t_waybill(up,tenant_id,create_time) select t.Index_ID,t.Index_CreatorCompanyID,t.Index_CreateTime from (SELECT a.Index_ID,Index_CreatorCompanyID,Index_CreateTime from TMS_OrderIndex a where a.Index_SrcClass = 1 and a.Index_SrcOrderID = 0 and a.Index_Status <> -1 " +
            "union all " +
            "select a.Index_ID,Index_CreatorCompanyID,Index_CreateTime from TMS_OrderIndex a where a.Index_SrcClass = 2 and a.Index_Status <> -1) t order by t.Index_ID asc")
    void init();

    @Update("update t_waybill set down = t2.Index_ID " +
            "from t_waybill t1,(select a.Index_ID as Index_ID,a.Index_SrcOrderID as Index_SrcOrderID from TMS_OrderIndex a where a.Index_SrcClass = 1 and a.Index_SrcOrderID > 0 and a.Index_Status <> -1 and a.Index_Status <> 32) as t2 where up = t2.Index_SrcOrderID;")
    void update();


    @Update("insert into t_waybill(up,tenant_id,create_time) select t.Index_ID,t.Index_CreatorCompanyID,t.Index_CreateTime from (SELECT a.Index_ID,Index_CreatorCompanyID,Index_CreateTime from TMS_OrderIndex a where a.Index_SrcClass = 1 and a.Index_SrcOrderID = 0 and a.Index_Status <> -1 and a.Index_CreateTime >= #{startTime} and a.Index_CreateTime <= #{endTime} " +
            "union all " +
            "select a.Index_ID,Index_CreatorCompanyID,Index_CreateTime from TMS_OrderIndex a where a.Index_SrcClass = 2 and a.Index_Status <> -1 and a.Index_CreateTime >= #{startTime} and a.Index_CreateTime <= #{endTime}) t order by t.Index_ID asc")
    void initTime(@Param("startTime") String startTime, @Param("endTime") String endTime);

}
